package org.jeecg.modules.doctor;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.modules.doctor.entity.Doctor;
import org.jeecg.common.modules.doctor.entity.DoctorAssistantRelation;
import org.jeecg.common.modules.doctor.service.IDoctorAssistantRelationService;
import org.jeecg.common.modules.doctor.service.IDoctorService;

import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.modules.doctor.vo.AssistantInfoReqVo;
import org.jeecg.common.modules.order.entity.OrderInfo;
import org.jeecg.common.modules.wx.vo.WxUserInfoReqVo;
import org.jeecg.modules.system.vo.AssistantDoctorResponseVo;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.system.vo.OrderInfoVo;
import org.jeecg.modules.system.vo.ResultVo;
import org.jeecg.modules.system.vo.WuserOrderInfoResponseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: doctor_assistant_relation
 * @Author: jeecg-boot
 * @Date:   2020-08-25
 * @Version: V1.0
 */
@Api(tags="doctor_assistant_relation")
@RestController
@RequestMapping("/doctor/doctorAssistantRelation")
@Slf4j
public class DoctorAssistantRelationController extends JeecgController<DoctorAssistantRelation, IDoctorAssistantRelationService> {
	@Autowired
	private IDoctorAssistantRelationService doctorAssistantRelationService;

	@Autowired
	private IDoctorService doctorService;

	/**
	 * 分页列表查询
	 *
	 * @param doctorAssistantRelation
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "doctor_assistant_relation-分页列表查询")
	@ApiOperation(value="doctor_assistant_relation-分页列表查询", notes="doctor_assistant_relation-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(DoctorAssistantRelation doctorAssistantRelation,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {

		//数据开始的记录数
		int dataStart = (pageNo - 1) * pageSize;

		//先查询所有的助理信息
		String name   = req.getParameter("name");
		String mobile = req.getParameter("mobile");
		String doctorName = req.getParameter("doctorName");
		List<AssistantInfoReqVo> assistantInfoReqVos = doctorService.getAssistantInfos(name,mobile,doctorName,dataStart,pageSize);

		//查询到的总数据数
		Integer count = doctorService.getCount(name,mobile,doctorName);

		//组合分页展示的数据
		ResultVo vo = this.initResultVo(assistantInfoReqVos,count,pageNo,pageSize);

		return Result.ok(vo);
	}


	 /**
	  * 组合返回的信息
	  * @param assistantInfoReqVos
	  * @param count
	  * @param pageNo
	  * @param pageSize
	  * @return
	  */
	 private ResultVo initResultVo(List assistantInfoReqVos, Integer count,Integer pageNo, Integer pageSize){
		 ResultVo vo = new ResultVo();
		 double dCount    = Double.valueOf(count);       //查询的数据总条数
		 double dPageSize = Double.valueOf(pageSize);    //每页显示的条数
		 int pages = (int) Math.ceil(dCount / dPageSize);//页码数量

		 vo.setCurrent(pageNo);
		 vo.setHitCount(false);
		 vo.setOptimizeCountSql(true);
		 vo.setOrders(new ArrayList<>());
		 vo.setRecords(assistantInfoReqVos);
		 vo.setPages(pages);
		 vo.setSearchCount(true);
		 vo.setSize(pageSize);
		 vo.setTotal(count);
		 return vo;
	 }


	 /**
	 *   添加
	 *
	 * @param doctorAssistantRelation
	 * @return
	 */
	@AutoLog(value = "doctor_assistant_relation-添加")
	@ApiOperation(value="doctor_assistant_relation-添加", notes="doctor_assistant_relation-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody DoctorAssistantRelation doctorAssistantRelation) {
		doctorAssistantRelationService.save(doctorAssistantRelation);
		return Result.ok("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param doctorAssistantRelation
	 * @return
	 */
	@AutoLog(value = "doctor_assistant_relation-编辑")
	@ApiOperation(value="doctor_assistant_relation-编辑", notes="doctor_assistant_relation-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody DoctorAssistantRelation doctorAssistantRelation) {
		doctorAssistantRelationService.updateById(doctorAssistantRelation);
		return Result.ok("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "doctor_assistant_relation-通过id删除")
	@ApiOperation(value="doctor_assistant_relation-通过id删除", notes="doctor_assistant_relation-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		doctorAssistantRelationService.removeById(id);
		return Result.ok("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "doctor_assistant_relation-批量删除")
	@ApiOperation(value="doctor_assistant_relation-批量删除", notes="doctor_assistant_relation-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.doctorAssistantRelationService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.ok("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "doctor_assistant_relation-通过id查询")
	@ApiOperation(value="doctor_assistant_relation-通过id查询", notes="doctor_assistant_relation-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		DoctorAssistantRelation doctorAssistantRelation = doctorAssistantRelationService.getById(id);
		if(doctorAssistantRelation==null) {
			return Result.error("未找到对应数据");
		}
		return Result.ok(doctorAssistantRelation);
	}

	 /**
	  * 通过助理id查询服务的医生
	  *
	  * @param assistantId
	  * @return
	  */
	 @AutoLog(value = "doctor_assistant_relation-通过assistant_id查询")
	 @ApiOperation(value="doctor_assistant_relation-通过assistant_id查询", notes="doctor_assistant_relation-通过assistant_id查询")
	 @GetMapping(value = "/doctorList")
	 public Result<?> doctorList(@RequestParam(name="id",required=true) String assistantId) {
	 	//查询助理所服务的医生
		List<DoctorAssistantRelation> relations = doctorAssistantRelationService.getRelation(assistantId);

		List response = new ArrayList();
		for(DoctorAssistantRelation relationOne : relations){
			//助理服务的单个医生
			Doctor doctor = doctorService.getById(relationOne.getDoctorId());
			AssistantDoctorResponseVo vo = this.initAssistantDoctorResponseVo(relationOne,doctor);
			response.add(vo);
		}
		return Result.ok(response);
	 }


	 /**
	  * 组合助理和医生信息
	  * @param relation
	  * @param doctor
	  * @return
	  */
	 private AssistantDoctorResponseVo initAssistantDoctorResponseVo(DoctorAssistantRelation relation,Doctor doctor){
		 AssistantDoctorResponseVo vo = new AssistantDoctorResponseVo();
		 vo.setId(doctor.getId());
		 vo.setName(doctor.getName());
		 vo.setStatus(relation.getStatus());
		 return vo;
	 }


	 /**
    * 导出excel
    *
    * @param request
    * @param doctorAssistantRelation
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, DoctorAssistantRelation doctorAssistantRelation) {
        return super.exportXls(request, doctorAssistantRelation, DoctorAssistantRelation.class, "doctor_assistant_relation");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, DoctorAssistantRelation.class);
    }

}
